package cn.iocoder.yudao.module.pinche.service;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.pinche.controller.admin.pincorder.vo.*;
import cn.iocoder.yudao.module.pinche.dal.dataobject.pincorder.PinCheOrderDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.pinche.convert.pincorder.PinCheOrderConvert;
import cn.iocoder.yudao.module.pinche.dal.mysql.pincorder.PinCheOrderMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.pinche.enums.ErrorCodeConstants.*;

/**
 * 拼车信息 Service 实现类
 *
 * @author 孙建辉
 */
@Service
@Validated
public class PinCheOrderServiceImpl implements PinCheOrderService {

    @Resource
    private PinCheOrderMapper pinCheOrderMapper;

    @Override
    public Long createPinCheOrder(PinCheOrderCreateReqVO createReqVO) {
        // 插入
        PinCheOrderDO pinCheOrder = PinCheOrderConvert.INSTANCE.convert(createReqVO);
        pinCheOrderMapper.insert(pinCheOrder);
        // 返回
        return pinCheOrder.getId();
    }

    @Override
    public void updatePinCheOrder(PinCheOrderUpdateReqVO updateReqVO) {
        // 校验存在
        validatePinCheOrderExists(updateReqVO.getId());
        // 更新
        PinCheOrderDO updateObj = PinCheOrderConvert.INSTANCE.convert(updateReqVO);
        pinCheOrderMapper.updateById(updateObj);
    }

    @Override
    public void deletePinCheOrder(Long id) {
        // 校验存在
        validatePinCheOrderExists(id);
        // 删除
        pinCheOrderMapper.deleteById(id);
    }

    private void validatePinCheOrderExists(Long id) {
        if (pinCheOrderMapper.selectById(id) == null) {
            throw exception(PIN_CHE_ORDER_NOT_EXISTS);
        }
    }

    @Override
    public PinCheOrderDO getPinCheOrder(Long id) {
        return pinCheOrderMapper.selectById(id);
    }

    @Override
    public List<PinCheOrderDO> getPinCheOrderList(Collection<Long> ids) {
        return pinCheOrderMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<PinCheOrderDO> getPinCheOrderPage(PinCheOrderPageReqVO pageReqVO) {
        return pinCheOrderMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PinCheOrderDO> getPinCheOrderList(PinCheOrderExportReqVO exportReqVO) {
        return pinCheOrderMapper.selectList(exportReqVO);
    }

}
